//
//  UIButton-Extension.swift
//  weiboswift
//
//  Created by dingzhibing on 2017/3/7.
//  Copyright © 2017年 dingzhibing. All rights reserved.
//

import UIKit
extension UIButton{

    static func textButton(title:String, fontSize:CGFloat, normalColor:UIColor, highlightedColor:UIColor,backgroundImageName:String?)->UIButton{
        let button = UIButton();
        button.setTitle(title, for: UIControlState.normal)
        button.setTitleColor(normalColor, for: UIControlState.normal)
        button.setTitleColor(highlightedColor, for: UIControlState.highlighted)
        button.sizeToFit()
        
        if let bgImageName = backgroundImageName {
            button.setBackgroundImage(UIImage(named:bgImageName), for: UIControlState.normal)
        }
        
        return button
    }
    
    func setBackgroundColor(backgroundColor:UIColor,for state: UIControlState){
        self .setBackgroundImage(UIButton.imageWithColor(color: backgroundColor), for: state)
    }
    
    static func imageWithColor(color:UIColor)->UIImage{
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
 }
